package cn.edu.zjgsu.ito.service.impl;

import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;

import cn.edu.zjgsu.ito.service.IPhotoUploadService;
import cn.edu.zjgsu.ito.util.FileUtil;

public class PhotoUploadServiceImpl implements IPhotoUploadService
{

	@Override
	public int uploadPhoto(MultipartFile file, String filename)
	{
		String extension = filename.split(".")[1];
		String[] limitExtensions = new String[]
		{ ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG", ".gif" };
		int i=0;
		int length = limitExtensions.length;
		for(i=0;i<length;i++)
		{
			if(extension.equals(limitExtensions[i]))
			{
				break;
			}
		}
		if(i==length)
		{
			return 0;
		}
		
		String newFilename = FileUtil.getRandomFileName()+"."+extension;
		
		try
		{
			FileUtil.savePhoto(file, newFilename);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		
		return 1;
	}

}
